<?php

/**
 * +----------------------------------------------------------------------
 * | TickyPHP [ This is a freeware ]
 * +----------------------------------------------------------------------
 * | Copyright (c) 2019 All rights reserved.
 * +----------------------------------------------------------------------
 * | Author: 罗敏贵 <e-mail:minguiluo@163.com> <QQ:271391233>
 * +----------------------------------------------------------------------
 * | SVN: $Id: Site.php 85779 2019-02-01 09:57:53 luomingui $
 * +----------------------------------------------------------------------
 * | 文件功能：对应的表名:tky_site
 * +----------------------------------------------------------------------
 */

namespace application\admin\controller;

use ticky\request;

class site extends base {

    // 首页
    public function index() {
        $search = $this->search_frm();
        $ret = db('site')->where($search['sql'])->order('siteid')->newpage();
        $theme_list = get_theme_all();
        $this->assign('theme_list', $theme_list);
        $this->assign('page', $ret['page']);
        $this->assign('items', $ret['items']);
        $this->assign('search', $search['arr']);
        $this->display('site/index');
    }

    // 添加记录
    public function add() {
        if (request::isPost()) {
            $data = $this->post_frm();
            db('site')->insert($data);
            cache('site', null);
            showmsg('添加成功', '/admin/site');
        } else {
            $theme_list = get_theme_all();
            $this->assign('theme_list', $theme_list);
            $this->assign('postUrl', '/admin/site/add');
            $this->assign('action', '添加');
            $this->display('site/manage');
        }
    }

    // 更新记录
    public function update() {
        if (request::isPost()) {
            $data = $this->post_frm();
            $id = request::post('siteid', 0);
            db('site')->where(['siteid' => $id])->update($data);
            cache('site', null);
            showmsg('修改成功', '/admin/site');
        } else {
            $siteid = request::get('siteid', 0);
            $item = db('site')->where(['siteid' => $siteid])->find();
            $this->assign('postUrl', '/admin/site/update');
            $this->assign('action', '修改');
            $this->assign('item', $item);
            $theme_list = get_theme_all();
            $this->assign('theme_list', $theme_list);
            $this->display('site/manage');
        }
    }

    // 删除记录
    public function delete() {
        $siteid = request::get('siteid', 0);
        $result = db('site')->delete(['siteid' => $siteid]);
        showmsg('删除成功', '/admin/site');
    }

    // 批量删除
    public function batchremove() {
        $optype = request::post('optype', 'del');
        $ids = request::post('ids', []);
        if ($optype == "del") {
            for ($i = 0; $i < count($ids); $i++) {
                $id = $ids[$i];
                db('site')->delete(['siteid' => $id]);
            }
            showmsg('删除成功', '/admin/site');
        }
    }

    //查询条件
    private function search_frm() {
        $search = request::get('search', []);
        $where = '1=1 ';

        $type = request::get('type', '');
        $keyword = request::get('keyword', '');
        $search['type'] = $type;
        if ($keyword != '') {
            $search['keyword'] = $keyword;
            switch ($type) {
                case "siteid":
                    $where .= "and siteid = '" . $keyword . "' ";
                    break;
                case "name":
                    $where .= "and name = '" . $keyword . "' ";
                    break;
                case "dirname":
                    $where .= "and dirname = '" . $keyword . "' ";
                    break;
                case "domain":
                    $where .= "and domain = '" . $keyword . "' ";
                    break;
                case "site_title":
                    $where .= "and site_title = '" . $keyword . "' ";
                    break;
                case "keywords":
                    $where .= "and keywords = '" . $keyword . "' ";
                    break;
                case "description":
                    $where .= "and description = '" . $keyword . "' ";
                    break;
                case "release_point":
                    $where .= "and release_point = '" . $keyword . "' ";
                    break;
                case "default_style":
                    $where .= "and default_style = '" . $keyword . "' ";
                    break;
                case "template":
                    $where .= "and template = '" . $keyword . "' ";
                    break;
                case "setting":
                    $where .= "and setting = '" . $keyword . "' ";
                    break;
                case "inputtime":
                    $where .= "and inputtime = '" . $keyword . "' ";
                    break;
            }
        }
        return [
            'arr' => $search,
            'sql' => $where
        ];
    }

    //表单数据
    private function post_frm() {
        $arr = array();
        $arr['name'] = request::post('name', '');
        $arr['dirname'] = request::post('dirname', '');
        $arr['domain'] = request::post('domain', '');
        $arr['site_title'] = request::post('site_title', '');
        $arr['keywords'] = request::post('keywords', '');
        $arr['description'] = request::post('description', '');
        $arr['release_point'] = request::post('release_point', '');
        $arr['default_style'] = request::post('default_style', '');
        $arr['template'] = request::post('template', '');
        $arr['setting'] = request::post('setting', '');
        $arr['inputtime'] = time();
        return $arr;
    }

}
